﻿using System.Windows;
using System.Windows.Controls;

namespace TotalApplication.AngelSix
{
    /// <summary>
    /// For extending margin to all children of a panel
    /// and keeps the navigation history empty
    /// </summary>
    public class PanelChildMarginProperty : BaseAttachedProperty<PanelChildMarginProperty, string>
    {
        public override void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var element = (Panel)sender;

            element.Loaded += (ss, ee) =>
            {
                foreach (FrameworkElement child in element.Children)
                    // Convert string to thickness and set margin
                    child.Margin = new ThicknessConverter().ConvertFromString((string)e.NewValue) as Thickness? ?? new Thickness(0);
            };
        }
    }
}
